﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Threading;
using EasyStudy.Logic.Remind;
using EasyStudy.Logic.Email;
using EasyStudy.Properties;

namespace EasyStudy.UI
{
    public partial class FrmSendEmail : KryptonForm
    {
        public FrmSendEmail()
        {
            InitializeComponent();

            Settings settings = new Settings();
            //this.txtEmailAddress.Text = settings.EmailTo;
            if (this.txtEmailAddress.Text.Length > 0)
            {
                this.txtEmailAddress.Select(0, 0);
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (EasyStudy.tools.Validate.ValidateEmail(txtEmailAddress.Text))
            {
                this.txtEmailAddress.Enabled = false;
                this.btnSend.Enabled = false;
                GlobalSettings.EmailTo = this.txtEmailAddress.Text;
                ThreadPool.QueueUserWorkItem(new WaitCallback(Thead4SendEmail));
            }
            else
            {
                MessageBox.Show("请输入正确的邮箱！",
                    "邮箱格式错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                txtEmailAddress.Focus();
            }
        }

        private void Thead4SendEmail(object a)
        {
            try
            {
                new RemindCheck().CheckNoShowForm();
                if (RemindCheck.TasksNeedToUpdate == null)
                {
                    MessageBox.Show(this, "当前任务为空！");
                    return;
                }

                SendTasks.SendTasksEmail(RemindCheck.TasksNeedToUpdate,
                    RemindCheck.TasksNeedToUpdate.Count + "个任务需要Update!");

                MessageBox.Show(this, "邮件发送成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "邮件发送失败！", "失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                this.txtEmailAddress.Enabled = true;
                this.btnSend.Enabled = true;
            }
        }
    }
}
